package ad.android.service.impl;

import ad.android.dao.IAdAppStatisticsDao;
import ad.android.exception.DaoException;
import ad.android.exception.ServiceException;
import ad.android.model.Ad;
import ad.android.model.AdApplicationStatistics;
import ad.android.model.Application;
import ad.android.service.IAdAppStatisticsService;
import ad.android.service.IAdService;
import ad.android.service.IApplicationService;
import ad.android.service.base.BaseService;
import ad.android.util.Constants;

public class AdAppStatisticsServiceImpl extends BaseService implements
		IAdAppStatisticsService {
	private IAdAppStatisticsDao adAppStatisticsDao;
	private IAdService adService;
	private IApplicationService appService;
	@Override
	public void addDownloadNum(Long appId, Long adId, int num)
			throws ServiceException {
		try {
			AdApplicationStatistics adAppStatistics = adAppStatisticsDao.findAdAppStatistics(appId,adId);
			if( adAppStatistics == null){
				adAppStatistics = new AdApplicationStatistics();

				Ad ad = adService.findAd(adId);
				if( ad == null){
					log.error("Ad is null. ad id:" + adId);
					throw new ServiceException("Ad is null. ad id:" + adId);
				}
				
				adAppStatistics.setTblAd(ad);
				
				Application app = appService.findApplication(appId);
				if( app == null){
					log.error("Application is null. app id:" + appId);
					throw new ServiceException("Application is null. app id:" + appId);
				}
				
				adAppStatistics.setTblApplication(app);
				adAppStatistics.setClickNum(Constants.NUM_0);
				adAppStatistics.setDownloadNum( num );

				adAppStatisticsDao.saveOrUpdate(adAppStatistics);
//				log.error("adAppStatistics is null.appId:" + appId + ",adId:"+ adId);
//				throw new ServiceException("adAppStatistics is null.appId:" + appId + ",adId:"+ adId);
			}else{
				adAppStatistics.setDownloadNum(adAppStatistics.getDownloadNum()+num);
				adAppStatisticsDao.saveOrUpdate(adAppStatistics);
			}
			
		} catch (DaoException e) {
			log.error("addDownloadNum",e);
			throw new ServiceException(e);
		}
	}

	@Override
	public void addClickNum(Long appId, Long adId, int num)
			throws ServiceException {
		try {
			AdApplicationStatistics adAppStatistics = adAppStatisticsDao.findAdAppStatistics(appId,adId);
			if( adAppStatistics == null){
				//there is no record in db,create one
				adAppStatistics = new AdApplicationStatistics();

				Ad ad = adService.findAd(adId);
				if( ad == null){
					log.error("Ad is null. ad id:" + adId);
					throw new ServiceException("Ad is null. ad id:" + adId);
				}
				
				adAppStatistics.setTblAd(ad);
				
				Application app = appService.findApplication(appId);
				if( app == null){
					log.error("Application is null. app id:" + appId);
					throw new ServiceException("Application is null. app id:" + appId);
				}
				
				adAppStatistics.setTblApplication(app);
				adAppStatistics.setClickNum(num);
				adAppStatistics.setDownloadNum( Constants.NUM_0 + num);

				adAppStatisticsDao.saveOrUpdate(adAppStatistics);
//				log.error("adAppStatistics is null.appId:" + appId + ",adId:"+ adId);
//				throw new ServiceException("adAppStatistics is null.appId:" + appId + ",adId:"+ adId);
			}else{
				adAppStatistics.setClickNum(adAppStatistics.getClickNum()+num);
				adAppStatisticsDao.saveOrUpdate(adAppStatistics);
			}
			
		} catch (Exception e) {
			log.error("addClickNum",e);
			throw new ServiceException(e);
		}
		
	}

	public void setAdAppStatisticsDao(IAdAppStatisticsDao adAppStatisticsDao) {
		this.adAppStatisticsDao = adAppStatisticsDao;
	}

	public void setAdService(IAdService adService) {
		this.adService = adService;
	}

	public void setAppService(IApplicationService appService) {
		this.appService = appService;
	}
}

